package com.example.hudongwx.food.fragment;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.hudongwx.food.R;
import com.example.hudongwx.food.utils.API;
import com.example.hudongwx.food.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区fragment
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_home_page)
public class HomePageFragment extends Fragment {
    private String TAG = "HomePageFragment";
    @ViewInject(R.id.home_recycler_view)
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        loadViewPagerImages();
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//        recyclerView.setLayoutManager(linearLayoutManager);
//        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(images);
//        recyclerView.setAdapter(adapter);

        return view;
    }

    private void loadViewPagerImages() {


        RequestParams params = new RequestParams(API.getViewPagerImg());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {

                try {
                    JSONArray ja= result.getJSONArray("Data");
                    List<ImageView> imageviews1 = getImageViewList(ja);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(linearLayoutManager);
                    MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(imageviews1);
                    recyclerView.setAdapter(adapter);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "onSuccess: " + result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i(TAG, "onCancelled: ");
            }

            @Override
            public void onFinished() {
                Log.i(TAG, "onFinished: ");
            }
        });
    }

    private List<ImageView> getImageViewList(JSONArray ja) throws JSONException {
        List<ImageView> imageviews1=new ArrayList<ImageView>();
        for (int i= 0;i<ja.length();i++){
            ImageView imageview=new ImageView(HomePageFragment.this.getActivity());
            String url =ja.getJSONObject(i).getString("Url");
            x.image().bind(imageview, url, new Callback.CommonCallback<Drawable>() {
                @Override
                public void onSuccess(Drawable result) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
            imageviews1.add(imageview);
        }
        return imageviews1;
    }


    class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<ImageView> images = null;

        public MyRecyclerViewAdapter(List<ImageView> images) {
            this.images = images;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder viewHolder = null;
            View view = null;
            switch (viewType) {
                case 0:
                    view = getActivity().getLayoutInflater().inflate(R.layout.viewpager_item, null);
                    viewHolder = new MyViewpagerRollViewHolder(view);
                    break;
            }

            return viewHolder;
        }

        //设置原点
        private void setView(MyViewpagerRollViewHolder viewHolder) {
            for (int i = 0; i < images.size(); i++) {
                View point = new View(getActivity());
                LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(25, 25);
                layoutParams.leftMargin = 10;
                point.setBackgroundResource(R.drawable.select_point_background);
                point.setLayoutParams(layoutParams);
                point.setEnabled(false);
                viewHolder.linearLayout.addView(point);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            switch (position) {
                case 0:
                    MyViewpagerRollViewHolder myHolder = (MyViewpagerRollViewHolder) holder;
                    setView(myHolder);
                    MyPagerAdapter pagerAdapter=new MyPagerAdapter(images);
                    myHolder.viewPager.setAdapter(pagerAdapter);
                    break;
            }
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }


        @Override
        public int getItemCount() {
            return 1;
        }
    }


    /**
     * 图片轮番viewPager的适配器
     */
    class MyPagerAdapter extends PagerAdapter {
        private List<ImageView> mList;

        public MyPagerAdapter(List<ImageView> mList) {
            this.mList = mList;
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            position%=mList.size();
            if (position<0){
                position=position+mList.size();
            }

            ImageView imageView = mList.get(position);
            ViewParent viewParent = imageView.getParent();
            if (viewParent!=null){
                ViewGroup group= (ViewGroup) viewParent;
                group.removeView(imageView);
            }
            container.addView(imageView);
            return imageView;
        }
    }

    /**
     *首页的recyclerView第一个item的viewHolder
     */
    class MyViewpagerRollViewHolder extends RecyclerView.ViewHolder {
        public ViewPager viewPager;
        public LinearLayout linearLayout;

        public MyViewpagerRollViewHolder(View itemView) {
            super(itemView);
            this.viewPager = (ViewPager) itemView.findViewById(R.id.viewpager_roll);
            this.linearLayout = (LinearLayout) itemView.findViewById(R.id.linear_roll);
        }


    }

}
